/**
 * 工程名: HelloMyFriend
 * 文件名: SaxParseHandler.java
 * 包名: com.stdroom.hellomyfriend.common
 * 日期: 2013-5-5下午12:02:14
 * Copyright (c) 2013, 北京联龙博通 All Rights Reserved.
 *
*/

package com.stdroom.hellomyfriend.common;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.stdroom.hellomyfriend.bean.ShortMessage;

/**
 * 类名: SaxParseHandler <br/>
 * 功能: TODO 添加功能描述. <br/>
 * 日期: 2013-5-5 下午12:02:14 <br/>
 *
 * @author   "lx"
 * @version  	 
 */
public class SaxParseHandler extends DefaultHandler{

	ArrayList<ShortMessage> messages = null;
	private ShortMessage messageItem = null;
	private String tagName = "";
	
	public ArrayList<ShortMessage> getMessage(){
		return messages;
	}

	@Override
	public void startDocument() throws SAXException {	//文档开始
		messages = new ArrayList<ShortMessage>(0);
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String data = new String(ch,start,length);
		if(tagName.equals("date")){
			messageItem.setDate(data);
		}else if(tagName.equals("time")){
			messageItem.setTime(data);
		}else if(tagName.equals("data")){
			messageItem.setData(data);
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(localName.equals("item")){
			messageItem = new ShortMessage();
			messageItem.setId(attributes.getValue("", "id"));
		}
			tagName = localName;
	}

	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equals("item")){
			messages.add(messageItem);
		}
		tagName = "";
	}
	
	@Override
	public void endDocument() throws SAXException {	//文档结束
		
	}


	
}

